/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.erp.client.widget;

import com.erp.client.DAO.ServerUserBaseAsync;
import com.erp.client.DAO.ServerUserBaseGetService;
import com.erp.client.DAO.UserBaseInf;
import com.erp.client.DAO.UserPasswordChange;
import com.erp.client.MessAge.myMessage;
import com.erp.client.MessAge.myMesswindow;
import com.erp.client.systemEntryPoint;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 *
 * @author Administrator
 */
public class myChangePassword extends Dialog {

    private myMessage mess = new myMessage();
    private String Stringreturn = "";
    protected ServerUserBaseAsync customerService;
    protected BeanModel customerModel;
    private myMesswindow messdisp = new myMesswindow();
    UserPasswordChange userpasswordchange = new UserPasswordChange();

    public myChangePassword() {
        final Window w = new Window();

        w.setMinimizable(false);
        w.setMaximizable(false);
        w.setResizable(false);
        w.setIconStyle("bogus");
        w.setPlain(true);
        w.setSize(320, 150);
        w.setHeading("系统设置：更改密码");
        w.setLayout(new FitLayout());

        final FormPanel panel = new FormPanel();
        panel.setBorders(false);
        panel.setBodyBorder(false);
        panel.setLabelWidth(100);
        //panel.setPadding(5);
        panel.setHeaderVisible(false);
        panel.reset();

        final TextField<String> oldpassword = new TextField<String>();
        oldpassword.setFieldLabel("请输入现用密码");
        oldpassword.setMaxLength(12);
        oldpassword.setPassword(true);
        panel.add(oldpassword, new FormData("100%"));

        final TextField<String> newpassword = new TextField<String>();
        newpassword.setFieldLabel("请输入新密码");
        newpassword.setMinLength(1);
        newpassword.setMaxLength(12);
        newpassword.setPassword(true);
        panel.add(newpassword, new FormData("100%"));

        final TextField<String> renewpassword = new TextField<String>();
        renewpassword.setFieldLabel("请输入新密码");
        renewpassword.setMinLength(1);
        renewpassword.setMaxLength(12);
        renewpassword.setPassword(true);
        panel.add(renewpassword, new FormData("100%"));

        customerModel = BeanModelLookup.get().getFactory(UserPasswordChange.class).createModel(userpasswordchange);
        final FormBinding formBinding = new FormBinding(panel, true);
        formBinding.bind(customerModel);
        customerModel.set("userId", systemEntryPoint.StaticEmpID);



        Button changepassowrdOK = new Button("确定");

        changepassowrdOK.addSelectionListener(new SelectionListener<ComponentEvent>() {

            @Override
            public void componentSelected(ComponentEvent ce) {
                if (oldpassword.getValue() != null && (newpassword.getValue() == renewpassword.getValue())) {
                    Info.display(mess.systemMess1, "开始更改密码!");
                    UserPasswordChange ups = new UserPasswordChange();
                    ups.setUserId(systemEntryPoint.StaticEmpID);
                    ups.setPassword(oldpassword.getValue());
                    ups.setNewpassword(newpassword.getValue());
                    AsyncCallback callback = new AsyncCallback() {

                        @Override
                        public void onSuccess(Object result) {
                            
                            if (((String)result) == "Success") {
                                oldpassword.setValue("");
                                newpassword.setValue("");
                                renewpassword.setValue("");
                                w.close();
                                Info.display("系统提示", "密码更改成功! ");
                            }
                            if (((String)result) == "Failure"){
                                //messdisp.myMessWindow("系统提示","原有密码错误，请检查！");
                                Info.display("系统提示","原有密码错误，请检查！");
                            }
                        }
                        @Override
                        public void onFailure(Throwable caught) {
                            Info.display("server", "密码更改失败!");
                        }
                    };
                    ServerUserBaseGetService.getService().userchangepassword(ups, callback);
                } else {
                    Info.display(mess.systemMess1, "请检查密码输入！");
                }
            }
        });

        Button changepassowrdNO = new Button("重置");
        changepassowrdNO.addSelectionListener(new SelectionListener<ComponentEvent>() {

            @Override
            public void componentSelected(ComponentEvent ce) {
                Info.display(mess.systemMess2, "密码数据已被清除！");
                panel.reset();
            }
        });

        w.addButton(changepassowrdOK);
        w.addButton(changepassowrdNO);
        w.add(panel);

        w.show();
    }
}
